

/* InstallScript
 * -------------
 * Register the HyperLink transport and install the Notepad patch.
 *
 */
 
InstallScript := func(partFrame) begin
	local prefs := try GetAppPrefs(kAppSymbol, nil) onexception |evt.ex| do prefs := {};
	
	partFrame.removeFrame := (partFrame.theForm):NewtInstallScript(partFrame.theForm);
	
		// Register the HyperLink Transport
	
	RegTransport(kHyperLinkTransportSymbol, partFrame.(kHyperLinkTransportSymbol));
		
		// Patch Notepad if the pref 'replaceNotepad is checked and HyperNewt is on the internal store
	
	if prefs and prefs.(kAppSymbol).replaceNotepad and GetPkgRef(kAppString, GetStores()[0]) then begin			
		local notes := GetView(GetRoot().paperroll);
		local notesProto := GetRoot().paperroll._proto;
		local HyperNewt := Clone(partFrame.theForm);

		if notes then GetRoot().paperroll:_Close();
		HyperNewt.(EnsureInternal('tag)) := kAppSymbol;
		HyperNewt.(EnsureInternal('_proto)) := notesProto;
		GetRoot().paperroll.(EnsureInternal('_proto)) := HyperNewt;
		
		if notes then GetRoot().paperroll:_Open();
		
		AddDeferredCall(func() GetRoot().paperroll:Install(), nil);
	end;
	else if prefs and prefs.(kAppSymbol).replaceNotepad and not GetPkgRef(kAppString, GetStores()[0]) then begin
		prefs.(kAppSymbol).replaceNotepad := nil;
		EntryChange(prefs);
		GetRoot():Notify(kNotifyQAlert, kAppTitle, LocObj("", '[pathExpr: messages, storeHyperNewtInternal]));
		AddDeferredCall(func() GetRoot().(kAppSymbol):Install(), nil);
	end;
	else AddDeferredCall(func() GetRoot().(kAppSymbol):Install(), nil);
end;


/* RemoveScript
 * ------------
 * Remove the notepad patch if necessary.
 *
 */

RemoveScript := func(partFrame) begin
	local removeFrame := partFrame.removeFrame;
	local prefs := try GetAppPrefs(kAppSymbol, nil) onexception |evt.ex| do prefs := {};
	local button := GetRoot().(kMinimizeViewSymbol);
	
	call removeFrame.NewtRemoveScript with (removeFrame);
	
	
		// Remove the Universal HyperNewt button
	
	if button and Visible(button) then button:Close();
	RemoveSlot(GetRoot(), kMinimizeViewSymbol);
	
	
		// Unregister soups and store changes
	
	UnRegSoupChange(ROM_paperrollSoupName, kSoupChangedSymbol);
	UnRegSoupChange(kHNDirectorySoupName, kSoupChangedSymbol);
	UnRegStoreChange(kStoreChangedSymbol);
	UnRegUnionSoup(kHNDirectorySoupName, kAppSymbol);
	UnRegUnionSoup(kRecentsSoupName, kAppSymbol);
	
	
		// Unregister HyperLink Transport
		
	UnRegTransport(EnsureInternal(kHyperLinkTransportSymbol));	
	
	
		// Unpatch Notepad
	
	if prefs.(kAppSymbol).replaceNotepad then begin	
		local notes := GetRoot().paperroll;
		local notesProto := notes._proto;
		local notesOpen := GetView(notes);
						
		if notesOpen then notes:_Close();
			
		while notesProto.tag do begin
			if HasSlot(notesProto, 'tag) and notesProto.tag = kAppSymbol then begin
				notes._proto := notes._proto._proto;
				break;
			end;
			notes := notesProto;
			notesProto := notesProto._proto;
			notesProto;
		end;

			// Reboot the Newton
		
		AddDeferredCall(func() AsyncConfirm(LocObj("", '[pathExpr: messages, reboot]), 'okCancelDefaultOk, func(val)
			if val then Reboot()), nil);
	end;
end;